package com.zhongli.devplatform.controller.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zhongli.devplatform.dto.sys.HelpDescriptionDTO;
import com.zhongli.devplatform.po.sys.HelpDescriptionPO;
import com.zhongli.devplatform.service.imp.sys.IHelpDescriptionService;
import com.zhongli.devplatform.utils.BeanCopierUtil;
import com.zhongli.devplatform.vo.sys.HelpDescriptionVO;
import com.zhongli.devplatform.bean.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/helpDescription")
public class HelpDescriptionController {


    @Autowired
    private IHelpDescriptionService helpDescriptionService;

    @RequestMapping("/list")
    public Res list(Long roleId) {

        List<HelpDescriptionPO> helpDescriptionMenuPOList = helpDescriptionService.list(new QueryWrapper<HelpDescriptionPO>().eq("role_id", roleId).isNull("parent_id"));
        List<HelpDescriptionVO> helpDescriptionMenuVOList = BeanCopierUtil.copy(helpDescriptionMenuPOList, HelpDescriptionVO.class);
        for (HelpDescriptionVO helpDescriptionMenuVO : helpDescriptionMenuVOList) {
            listChildren(helpDescriptionMenuVO);
        }
        return new Res(helpDescriptionMenuVOList);
    }


    private void listChildren(HelpDescriptionVO helpDescriptionVO) {
        List<HelpDescriptionPO> helpDescriptionPOList = helpDescriptionService.list(new QueryWrapper<HelpDescriptionPO>().eq("parent_id", helpDescriptionVO.getId()));
        if (helpDescriptionPOList.isEmpty()) {
            return;
        }

        List<HelpDescriptionVO> helpDescriptionMenuVOList = BeanCopierUtil.copy(helpDescriptionPOList, HelpDescriptionVO.class);
        helpDescriptionVO.setChildren(helpDescriptionMenuVOList);
        for (HelpDescriptionVO child : helpDescriptionMenuVOList) {
            listChildren(child);
        }
    }


    @PostMapping("/save")
    public Res save(HelpDescriptionDTO helpDescriptionDTO) {

        if (helpDescriptionDTO.getId() == null) {
            HelpDescriptionPO helpDescriptionPO = BeanCopierUtil.copy(helpDescriptionDTO, HelpDescriptionPO.class);
            helpDescriptionService.save(helpDescriptionPO);
        } else {
            helpDescriptionService.update(new UpdateWrapper<HelpDescriptionPO>()
                    .set("name", helpDescriptionDTO.getName())
                    .set("parent_id", helpDescriptionDTO.getParentId())
                    .set("content", helpDescriptionDTO.getContent())
                    .set("video", helpDescriptionDTO.getVideo())
                    .eq("id", helpDescriptionDTO.getId())
            );

        }
        return new Res(null);
    }


    @RequestMapping("/delete")
    public Res delete(Long id) {
        helpDescriptionService.removeById(id);
        return new Res(null);
    }


    @RequestMapping("/listParents")
    public Res listParents(Long id, Boolean joinItself) {
        List<Long> parents = new ArrayList<>();
        if (joinItself) {
            parents.add(id);
        }
        getParent(id, parents);
        Collections.reverse(parents);
        return new Res(parents);
    }

    private void getParent(Long id, List<Long> parents) {
        Long parentId = helpDescriptionService.getById(id).getParentId();
        if (null == parentId) {
            return;
        }
        parents.add(parentId);
        getParent(parentId, parents);
    }


}
